package com.zy.edu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.edu.bean.CourseChapter;
import com.zy.edu.bean.DataConfig;
import com.zy.edu.bean.ResponseInfo;
import com.zy.edu.config.SystemParam;
import com.zy.edu.dao.DataConfigDao;
import com.zy.edu.service.DataConfigService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuangHaiShui
 * @since 2021-07-28
 */
@Service
public class DataConfigServiceImpl extends ServiceImpl<DataConfigDao, DataConfig> implements DataConfigService {

    @Override
    public Map getByName(String name) {
        if(StringUtils.isEmpty(name)) {
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }else {
            EntityWrapper entityWrapper =  new EntityWrapper<DataConfig>();
            entityWrapper.eq("name",name);
            List<DataConfig> list = this.baseMapper.selectList(entityWrapper);
            if(list.size()>0){
                return ResponseInfo.success(list.get(0).getValue());
            }
            return ResponseInfo.error("无该配置信息");
        }
    }

    @Override
    public String getValueByName(String name) {
        EntityWrapper entityWrapper =  new EntityWrapper<DataConfig>();
        entityWrapper.eq("name",name);
        List<DataConfig> list = this.baseMapper.selectList(entityWrapper);
        if(list.size()>0){
            return list.get(0).getValue();
        }
        return null;
    }

    @Override
    public Map getAli() {
        String aliRegionId = getValueByName("aliRegionId");
        String aliKeyId = getValueByName("aliKeyId");
        String aliEndpoint = getValueByName("aliEndpoint");
        String aliKeySecret = getValueByName("aliKeySecret");
        String aliBucket = getValueByName("aliBucket");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("region", aliRegionId);
        resultMap.put("keyId", aliKeyId);
        resultMap.put("keySecret", aliKeySecret);
        resultMap.put("bucket", aliBucket);
        resultMap.put("endpoint", aliEndpoint);
        return ResponseInfo.success(resultMap);
    }
}
